<template>
	<view class="cart-container" v-if="cart.length !== 0">
		
		<my-address></my-address>
	<!-- 购物车商品列表的标题区域 -->
	<view class="cart-title">
	  <!-- 左侧的图标 -->
	  <uni-icons type="shop" size="18"></uni-icons>
	  <!-- 描述文本 -->
	  <text class="cart-title-text">购物车</text>
	</view>
	
	
	<uni-swipe-action>
	  
	  

		   <uni-swipe-action-item  v-for="(goods, i) in cart" :key="i" :right-options="options" @click="swipeActionClickHandler(goods)">
	  <view >
	  	
	  	<!-- tofixed -->
	  	<view  class="goods-item">
	  	  <!-- 商品左侧图片区域 -->
	  	  <view class="goods-item-left">
	  		  <radio @click="radioChangeHandler(goods)" :checked="goods.goods_state" color="#C00000"></radio>
	  	    <image :src="goods.goods_small_logo || defaultPic" class="goods-pic"></image>
	  	  </view>
	  	  <!-- 商品右侧信息区域 -->
	  	  <view class="goods-item-right">
	  	    <!-- 商品标题 -->
	  	    <view class="goods-name">{{goods.goods_name}}</view>
	  	    <view class="goods-info-box">
	  	      <!-- 商品价格 -->
	  	      <view class="goods-price">￥{{goods.goods_price|tofixed}}</view>
	  		   <!-- 商品数量 -->
	  		    <uni-number-box :ref="'input_d'+i" @change="numberChangeHandler(goods)" :min="1"  :value="goods.goods_count">
					
				</uni-number-box>
	  	    </view>
	  	  </view>
	  	</view>
		
	  </view>
	    </uni-swipe-action-item>

	  
	    <!-- uni-swipe-action-item 可以为其子节点提供滑动操作的效果。需要通过 options 属性来指定操作按钮的配置信息 -->
	   
	     
	
	</uni-swipe-action>
	
	
	<my-settle></my-settle>
	</view>
	  <view class="empty-cart" v-else>
	   
	    <text class="tip-text">空空如也~</text>
	  </view>
</template>

<script>
	// 按需导入 mapState 这个辅助函数
	import { mapState,mapMutations  } from 'vuex'
	 import badgeMix from '@/mixins/index.js'
	 
	export default {
		
		mixins:[badgeMix],
		data() {
			return {
				
				defaultPic:'',
				options:[{
				      text: '删除', // 显示的文本内容
				      style: {
				        backgroundColor: '#C00000' // 按钮的背景颜色
				      }
				    }]
			};
		},
		 computed: {
		    // 将 m_cart 模块中的 cart 数组映射到当前页面中使用
		    ...mapState('cart', ['cart']),
		  },
		filters: {
		  // 把数字处理为带两位小数点的数字
		  tofixed(num) {
		    return Number(num).toFixed(2); 
		  }
		},
		methods: {
		   ...mapMutations('cart', ['updateGoodsState' ,'updateGoodsCount','removeGoodsById']),
		   // 点击了滑动操作按钮
		     swipeActionClickHandler(goods) {
				
		       this.removeGoodsById(goods.goods_id)
			     this.setBadge()
		     },
		   
		
		   // 商品的数量发生了变化
		   numberChangeHandler(goods) {
			    var nuim;
			   console.log(goods);
			   this.cart.find((t,i)=>{
				if(   goods.goods_id==t.goods_id
				   ){
					   nuim =this.$refs[`input_d${i}`][0].inputValue;
					   console.log(nuim);
				   }
			   })
			   
			
		
		console.log(nuim);
		if(nuim>100){
			uni.showToast({
				title: '超过最高下单量哦！',
				icon:'error',
				duration: 2000
			});
			
			setTimeout(function () {
				uni.hideLoading();
			}, 2000);
			return
		}
			   let data={
      // 商品的 Id
      goods_id: goods.goods_id,
      // 商品的最新数量
      goods_count:Number(nuim)
    }
		    this.updateGoodsCount(data)
			
			this.setBadge()
		   },
		   
		   
		 radioChangeHandler(goods) {
			 
			let data= {
			       // 商品的 Id
			       goods_id: goods.goods_id,
			       // 商品最新的勾选状态
			       goods_state: !goods.goods_state
			     }
				 this.updateGoodsState(data)
				 // console.log(data);
		 // 输出得到的数据 -> {goods_id: 395, goods_state: false}
		  }
	}}
</script>

<style lang="scss">
	.cart-container {
	  padding-bottom: 60px;
	}
.cart-title {
  height: 40px;
  display: flex;
  align-items: center;
  font-size: 14px;
  padding-left: 5px;
  border-bottom: 1px solid #efefef;
  .cart-title-text {
    margin-left: 10px;
  }
}.goods-item {
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;

  .goods-item-left {
    margin-right: 5px;

    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
    }

    .goods-price {
      font-size: 16px;
      color: #c00000;
    }
  }
}
.goods-item-left {
  margin-right: 5px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .goods-pic {
    width: 100px;
    height: 100px;
    display: block;
  }
}
.goods-item-right {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: space-between;

  .goods-name {
    font-size: 13px;
  }

  .goods-info-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .goods-price {
    font-size: 16px;
    color: #c00000;
  }
}
.goods-item {
  // 让 goods-item 项占满整个屏幕的宽度
  width: 750rpx;
  // 设置盒模型为 border-box
  box-sizing: border-box;
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;
}
.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 150px;

  .empty-img {
    width: 90px;
    height: 90px;
  }

  .tip-text {
    font-size: 12px;
    color: gray;
    margin-top: 15px;
  }
}
</style>
